package br.com.ProjetoTCC_JSF.modelo.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class CreateEntityManagerFactory {

	private Properties properties = new Properties();	
	private EntityManagerFactory emf;

	//Carregando as propriedades de um arquivo '.properties'
	public void initProperties() {
		FileInputStream in;
		try {
			in = new FileInputStream("C:\\DASistemas\\props.properties");
			properties.load(in);
			in.close();

		} catch (FileNotFoundException e) {
			System.out.println("File 'props.properties' not found !"+ e);
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("File 'props.properties' not found !"+ e);
			e.printStackTrace();
		}

	}
	
	//Criando meu entityManagerFactory
	public EntityManagerFactory createEntityManagerFactory() {
		initProperties();
		if (emf == null) {
			System.out.println("Appling properties to persistence....");
			
			Map<String, Object> configOverrides = new HashMap<String, Object>();
			configOverrides.put("hibernate.connection.username", properties.getProperty("username"));
			configOverrides.put("hibernate.connection.password", properties.getProperty("password"));
			configOverrides.put("hibernate.connection.url", properties.getProperty("url"));
			configOverrides.put("hibernate.connection.driver_class", properties.getProperty("driver_class"));
			configOverrides.put("hibernate.dialect", properties.getProperty("dialect"));
			configOverrides.put("hibernate.show_sql", properties.getProperty("show_sql"));
			configOverrides.put("hibernate.format_sql", properties.getProperty("format_sql"));
			configOverrides.put("hibernate.hbm2ddl.auto", properties.getProperty("hbm2ddl_auto"));

			System.out.println("Creating Entity Manager Factory....");
			return emf = Persistence.createEntityManagerFactory("DASistemas", configOverrides);
		} else {
			return emf;
		}
	}
}